#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/12 23:01
# @Author : 不想起名
# @Site : 
# @File : screenShot.py
# @Software: PyCharm
import ctypes
import tkinter
from PIL import ImageGrab


class screenShot:
    def __init__(self):
        self.image_path = 'temp/tmp.png'  # 截图保存路径
        self.tk = tkinter.Tk()
        self.tk.attributes("-alpha", 0.35)  # 设置窗口半透明
        self.tk.configure(bg='black')
        self.__start_x = 0
        self.__start_y = 0

        user32 = ctypes.windll.user32
        gdi32 = ctypes.windll.gdi32
        dc = user32.GetDC(None)
        widthScale = gdi32.GetDeviceCaps(dc, 8)  # 分辨率缩放后的宽度
        heightScale = gdi32.GetDeviceCaps(dc, 10)  # 分辨率缩放后的高度
        width = gdi32.GetDeviceCaps(dc, 118)  # 原始分辨率的宽度
        height = gdi32.GetDeviceCaps(dc, 117)  # 原始分辨率的高度
        self.__scale = (width / widthScale + height / heightScale) / 2

        self.__width = self.tk.winfo_screenwidth()
        self.__height = self.tk.winfo_screenheight()

        # 创建画布
        self.__canvas = tkinter.Canvas(self.tk, width=self.__width, height=self.__height, bg="black")

        self.tk.bind('<Button-1>', self.start)  # 绑定鼠标左键点击事件
        self.tk.bind('<ButtonRelease-1>', self.end)  # 绑定鼠标左键点击释放事件
        self.tk.bind('<B1-Motion>', self.draw_rectangle)  # 绑定鼠标左键点击移动事件
        self.tk.bind('<Button-3>', lambda e: self.tk.destroy())  # 绑定鼠标右键点击退出事件

        self.tk.attributes("-fullscreen", True)  # 设置全屏
        self.tk.attributes("-topmost", True)  # 设置窗口在最上层

        self.tk.mainloop()  # 窗口持久化

    def start(self, event):
        # print(event.state)
        self.__start_x, self.__start_y = event.x, event.y

    def end(self, event):
        self.tk.attributes("-alpha", 0)  # 设置窗口透明,避免闪烁
        self.tk.destroy()
        if event.x == self.__start_x or event.y == self.__start_y:
            return
        # 下面是对起始坐标和终止坐标4种情况的讨论
        elif event.x > self.__start_x and event.y < self.__start_y:
            im = ImageGrab.grab((self.__scale * self.__start_x, self.__scale * event.y,
                                 self.__scale * event.x, self.__scale * self.__start_y))
        elif event.x < self.__start_x and event.y < self.__start_y:
            im = ImageGrab.grab((self.__scale * event.x, self.__scale * event.y,
                                 self.__scale * self.__start_x, self.__scale * self.__start_y))
        elif event.x < self.__start_x and event.y > self.__start_y:
            im = ImageGrab.grab((self.__scale * event.x, self.__scale * self.__start_y,
                                 self.__scale * self.__start_x, self.__scale * event.y))
        else:
            im = ImageGrab.grab((self.__scale * self.__start_x, self.__scale * self.__start_y,
                                 self.__scale * event.x, self.__scale * event.y))
        im.save(self.image_path)

    def draw_rectangle(self, event):
        if event.x == self.__start_x or event.y == self.__start_y:
            return
        self.__canvas.delete("parser")
        # 画出区域框
        self.__canvas.create_rectangle(self.__start_x, self.__start_y, event.x, event.y,
                                       fill='lightgray', outline='red', tags='parser', width=3, dash=(4, 4))
        # 包装画布
        self.__canvas.pack()